<?php
/**
 * 支付
 *
 *
 *
 *
 * by 33hao.com 好商城V3 运营版
 */

//use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');

class member_paymentControl extends mobileMemberControl {

    private $payment_code = 'alipay';

	public function __construct() {
		parent::__construct();
        $this->payment_code = isset($_GET['payment_code']) && trim($_GET['payment_code']) != '' ? trim($_GET['payment_code']) :'alipay';
	}
function actestOp(){
    $inc_file = BASE_PATH.DS.'api'.DS.'payment'.DS.'acpay'.DS.'acpay.php';
      require($inc_file);
            $param = array();
            
            $payment_api = new $this->payment_code();
            $return = $payment_api->submit($param);
            echo $return;
}
    /**
     * 实物订单支付
     */
    public function payOp() {
	    $pay_sn = $_GET['pay_sn'];
	    if(empty($pay_sn)) {
	        $pay_sn = $_POST['pay_sn'];
	    }
        $model_mb_payment = Model('mb_payment');
        $logic_payment = Logic('payment');

        $condition = array();
        $condition['payment_code'] = $this->payment_code;
        $mb_payment_info = $model_mb_payment->getMbPaymentOpenInfo($condition);

        if(!$mb_payment_info) {
            output_error('系统不支持选定的支付方式');
        }
        //重新计算所需支付金额
        $result = $logic_payment->getRealOrderInfo($pay_sn, $this->member_info['member_id']);
        if(!$result['state']) {
            output_error($result['msg']);
        }
        //第三方API支付
        $this->_api_pay($result['data'], $mb_payment_info);
    }

    /**
     * 虚拟订单支付
     */
    public function vr_payOp() {
        $order_sn = $_GET['pay_sn'];
    
        $model_mb_payment = Model('mb_payment');
        $logic_payment = Logic('payment');
    
        $condition = array();
        $condition['payment_code'] = $this->payment_code;
        $mb_payment_info = $model_mb_payment->getMbPaymentOpenInfo($condition);
        if(!$mb_payment_info) {
            output_error('系统不支持选定的支付方式');
        }
    
        //重新计算所需支付金额
        $result = $logic_payment->getVrOrderInfo($order_sn, $this->member_info['member_id']);
    
        if(!$result['state']) {
            output_error($result['msg']);
        }
    
        //第三方API支付
        $this->_api_pay($result['data'], $mb_payment_info);
    }

	/**
	 * 第三方在线支付接口
	 *
	 */
	private function _api_pay($order_pay_info, $mb_payment_info) {
        $inc_file = BASE_PATH.DS.'api'.DS.'payment'.DS.$this->payment_code.DS.$this->payment_code.'.php';
    	if(!is_file($inc_file)){
            output_error('支付接口不存在');
    	}
		if($mb_payment_info['payment_code'] == 'wxpay') {
			/*$wxpayurl=BASE_SITE_URL.'/mobile/api/payment/wxpay/wxpay.php?pay_sn='.$order_pay_info['pay_sn'].'&pay_amount='.$order_pay_info['api_pay_amount'];
    		redirect($wxpayurl);*/
            require($inc_file);

            $param = array();
            $param = $mb_payment_info['payment_config'];
            $param['pay_sn'] = $order_pay_info['pay_sn'];
            $param['order_amount'] = $order_pay_info['api_pay_amount'];
            $param['order_type'] = ($order_pay_info['order_type'] == 'real_order' ? 'r' : 'v');

            $payment_api = new $this->payment_code();
            $return = $payment_api->submit($param);
            output_data(array('prepay_id' => $return));
    		//@header("Location: ".ApiUrl.'/api/paymentpaypay.php?pay_sn='.$order_pay_info['pay_sn'].'&pay_amount='.$order_pay_info['api_pay_amount']);
    	} else if($mb_payment_info['payment_code'] == 'alipay') {
			require($inc_file);
			$param = array();
			$param = $mb_payment_info['payment_config'];
			$param['order_sn'] = $order_pay_info['pay_sn'];
			$param['order_amount'] = $order_pay_info['api_pay_amount'];
			$param['order_type'] = ($order_pay_info['order_type'] == 'real_order' ? 'r' : 'v');
			$payment_api = new $this->payment_code();
			$return = $payment_api->submit($param);
			echo $return;
    	} else if($mb_payment_info['payment_code'] == 'bestpay') {
    	    require($inc_file);
    	    $param = array();
    	    
    	    /* $key = '883602';
			$datakey = 'B34B4C898EE12A1A91E2DB4F9177F5B33755106A12E65E37';
			$param['MERCHANTID']='02450103030134669';
			$param['ORDERSEQ']=$order_pay_info['pay_sn'];
			$param['ORDERREQTRANSEQ']=date('YmdHis').mt_rand(10000,99999);
			$param['ORDERDATE']=date('YmdHis');
			$param['ORDERAMOUNT']= number_format($order_pay_info['api_pay_amount'], 2, '.', '');
			$param['PRODUCTAMOUNT']= number_format($order_pay_info['api_pay_amount'], 2, '.', '');
			$param['ATTACHAMOUNT']='0.00';
			$param['CURTYPE']='RMB';
			$param['ENCODETYPE'] = "1";
			$param['MERCHANTURL'] = BASE_PATH.DS.'api'.DS.'payment'.DS.$this->payment_code.DS."mobile.backurl.php"; # Feedback Url
			$param['BACKMERCHANTURL'] = BASE_PATH.DS.'api'.DS.'payment'.DS.$this->payment_code.DS."mobile.backurl.php";
			$param['BUSICODE'] = "0001";
			$param['PRODUCTDESC'] = '实物订单';
			$param['PRODUCTID'] = '';
			$param['TMNUM'] = '';
			$param['CUSTOMERID'] = '';
			$macmd5='MERCHANTID='.$param['MERCHANTID'].'&ORDERSEQ='.$param['ORDERSEQ'].'&ORDERDATE='.$param['ORDERDATE'].'&ORDERAMOUNT='.$param['ORDERAMOUNT'].'&KEY='.$datakey;
			$param['MAC'] = md5($macmd5); */
// 			foreach($param as $key=>$value) {
// 			    echo $key.'=>'.$value.'<br>';
// 			}
			
			
			$param['order_sn'] = $order_pay_info['pay_sn'];
			$param['order_amount'] = number_format($order_pay_info['api_pay_amount'], 2, '.', '');
			$param['order_type'] = ($order_pay_info['order_type'] == 'real_order' ? 'r' : 'v');
			
			$payment = array();
			$payment['MERCHANTID']=$mb_payment_info['payment_config']['bestpay_partner'];
			$payment['KEY'] = $mb_payment_info['payment_config']['bestpay_datakey'];
			$payment['BESTPAY_PWD'] = $mb_payment_info['payment_config']['bestpay_key'];
			$payment['BACKURL'] = MOBILE_SITE_URL.DS.'api'.DS.'payment'.DS.$this->payment_code.DS."callbackurl.php";
			$payment_api = new $this->payment_code();
			$return = $payment_api->generate_pay_button($param, $payment);
			echo $return;
    	}
        else{
            //银联支付
                
            // echo  dirname(dirname(__FILE__)).'/certs/acp_test_sign.pfx';die;
            require($inc_file);
            $param = array();
            $param = $mb_payment_info['payment_config'];
            $param['order_sn'] = $order_pay_info['pay_sn'];
            $param['order_amount'] = $order_pay_info['api_pay_amount'];
            $param['order_type'] = ($order_pay_info['order_type'] == 'real_order' ? 'r' : 'v');
           // echo json_encode($param);die;
            $payment_api = new $this->payment_code();
            $return = $payment_api->submit($param);
            echo $return;
        }
    	exit();
	}

    /**
     * 可用支付参数列表
     */
    public function payment_listOp() {
        $model_mb_payment = Model('mb_payment');

        $payment_list = $model_mb_payment->getMbPaymentOpenList();

        $payment_array = array();
        if(!empty($payment_list)) {
            foreach ($payment_list as $value) {
                $payment_array[] = $value['payment_code'];
            }
        }

        output_data(array('payment_list' => $payment_array));
    }
}
